{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********** Start recording for 5 seconds! **********\n",
      "..........1s\n",
      "\n",
      "..........2s\n",
      "\n",
      "..........3s\n",
      "\n",
      "..........4s\n",
      "\n",
      "..........5s\n",
      "\n",
      "********** Stop recording! **********\n",
      "{\"refresh_token\":\"25.3caad53449be4f1a73b072c341d117d7.315360000.2060922622.282335-118650883\",\"expires_in\":2592000,\"session_key\":\"9mzdDc8ts9JOv4Beo8eQqWrw+SU78\\/NokPO7WtO1+2Cljo5Fo8Q8XGf70DL62dQvcBKzfG9LZQJkuCd4e5b03GaPSkbJmaI=\",\"access_token\":\"24.699fd1736379830a4e45afedd3fc6111.2592000.1748154622.282335-118650883\",\"scope\":\"public audio_voice_assistant_get audio_tts_post brain_all_scope brain_enhanced_asr brain_speech_realtime brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"54b487c63ec0701717037a9490951e89\"}\n",
      "\n",
      "{'refresh_token': '25.3caad53449be4f1a73b072c341d117d7.315360000.2060922622.282335-118650883', 'expires_in': 2592000, 'session_key': '9mzdDc8ts9JOv4Beo8eQqWrw+SU78/NokPO7WtO1+2Cljo5Fo8Q8XGf70DL62dQvcBKzfG9LZQJkuCd4e5b03GaPSkbJmaI=', 'access_token': '24.699fd1736379830a4e45afedd3fc6111.2592000.1748154622.282335-118650883', 'scope': 'public audio_voice_assistant_get audio_tts_post brain_all_scope brain_enhanced_asr brain_speech_realtime brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '54b487c63ec0701717037a9490951e89'}\n",
      "SUCCESS WITH TOKEN: 24.699fd1736379830a4e45afedd3fc6111.2592000.1748154622.282335-118650883 ; EXPIRES IN SECONDS: 2592000\n",
      "url is http://vop.baidu.com/server_api?cuid=123456PYTHON&token=24.699fd1736379830a4e45afedd3fc6111.2592000.1748154622.282335-118650883&dev_pid=1537\n",
      "header is {'Content-Type': 'audio/wav; rate=16000', 'Content-Length': 200044}\n",
      "Request time cost 0.930049\n",
      "打开通讯工程。\n",
      "{\"corpus_no\":\"7497134380247216976\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"打开通讯工程。\"],\"sn\":\"753285663261745562623\"}\n",
      "\n",
      "语音识别结果 = 打开通讯工程。\n",
      "fetch token begin\n",
      "{\"refresh_token\":\"25.b25c8da8226b20c453bac11ea28f5a04.315360000.2060922624.282335-118650883\",\"expires_in\":2592000,\"session_key\":\"9mzdDcSxWiyhorc0iff5ComDY9ue9VYN\\/FjvVyUpMmzeJxSIxoRLvTf55Vg48WOLKZSO+pb\\/RzQkaq4srMuOOAKpwJNPI0I=\",\"access_token\":\"24.6225935f33b9fcd9944f8449aae8c3d3.2592000.1748154624.282335-118650883\",\"scope\":\"public audio_voice_assistant_get audio_tts_post brain_all_scope brain_enhanced_asr brain_speech_realtime brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"019e0c4806aee5a6bda4a55184d26d03\"}\n",
      "\n",
      "{'refresh_token': '25.b25c8da8226b20c453bac11ea28f5a04.315360000.2060922624.282335-118650883', 'expires_in': 2592000, 'session_key': '9mzdDcSxWiyhorc0iff5ComDY9ue9VYN/FjvVyUpMmzeJxSIxoRLvTf55Vg48WOLKZSO+pb/RzQkaq4srMuOOAKpwJNPI0I=', 'access_token': '24.6225935f33b9fcd9944f8449aae8c3d3.2592000.1748154624.282335-118650883', 'scope': 'public audio_voice_assistant_get audio_tts_post brain_all_scope brain_enhanced_asr brain_speech_realtime brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '019e0c4806aee5a6bda4a55184d26d03'}\n",
      "SUCCESS WITH TOKEN: 24.6225935f33b9fcd9944f8449aae8c3d3.2592000.1748154624.282335-118650883 ; EXPIRES IN SECONDS: 2592000\n",
      "result saved as :result.wav\n",
      "********** Start playing! **********\n",
      "********** Stop playing! **********\n"
     ]
    }
   ],
   "source": [
    "#导入依赖包\n",
    "import asr_raw as ar  #百度语音识别包\n",
    "import webbrowser\n",
    "import tts as tts     #百度语音合成包\n",
    "from RecordPlay import recorder#录音和播放函数\n",
    "import os             #标准os库\n",
    "#录音5s,保存音频到test.wav文件\n",
    "r = recorder()\n",
    "r.recorder(\"test.wav\")\n",
    "#识别语音内容，并打印\n",
    "result_str = ar.Voice2Text(\"openwechat.wav\")\n",
    "print(\"语音识别结果 = \" + result_str)#打印识别结果\n",
    "#判断识别文本内容，并执行相应的操作\n",
    "if \"打开计算器\" in result_str:\n",
    "    os.startfile(\"calc.exe\")\n",
    "    text = \"主人，已经打开计算器。\"\n",
    "elif \"打开记事本\" in result_str:\n",
    "    os.startfile(\"notepad.exe\") \n",
    "    text = \"主人，已经打开记事本。\"\n",
    "elif \"打开画图板\" in result_str:\n",
    "    os.startfile(\"mspaint.exe\")\n",
    "    text = \"主人，已经打开画图板。\"\n",
    "elif \"打开通讯工程\" in result_str:\n",
    "    webbrowser.open(\"https://gitee.com/zhensi-rong\")\n",
    "    text = \"主人，已经打开gitee。\"\n",
    "elif \"你的名字\" in result_str:\n",
    "    text = \"我的名字是龙炜轩的助手。\"    \n",
    "else:\n",
    "    text = \"对不起，我还不能理解这个任务！\"\n",
    "#语音合成     \n",
    "file = tts.Text2Voice(text)\n",
    "#播放语音文件\n",
    "r.playwav(file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 连续打开多应用则修改分支"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********** Start recording for 5 seconds! **********\n",
      "..........1s\n",
      "\n",
      "..........2s\n",
      "\n",
      "..........3s\n",
      "\n",
      "..........4s\n",
      "\n",
      "..........5s\n",
      "\n",
      "********** Stop recording! **********\n",
      "{\"refresh_token\":\"25.71250d20e015c9678b13d80d9590b17c.315360000.2060923830.282335-118650883\",\"expires_in\":2592000,\"session_key\":\"9mzdAqPkluYtZw2PK8MmLqHeHZOOGwAu3thAvS15+uD3mrAdulel8AP21Es1kWDxO8W0nlgjZtUa4bkwo6OB+EjS8G27kG0=\",\"access_token\":\"24.9ab13b8748e7c17fbe8cdc8430a0f181.2592000.1748155830.282335-118650883\",\"scope\":\"public audio_voice_assistant_get audio_tts_post brain_all_scope brain_enhanced_asr brain_speech_realtime brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"71839250416a64b057fdfb5a71269bbf\"}\n",
      "\n",
      "{'refresh_token': '25.71250d20e015c9678b13d80d9590b17c.315360000.2060923830.282335-118650883', 'expires_in': 2592000, 'session_key': '9mzdAqPkluYtZw2PK8MmLqHeHZOOGwAu3thAvS15+uD3mrAdulel8AP21Es1kWDxO8W0nlgjZtUa4bkwo6OB+EjS8G27kG0=', 'access_token': '24.9ab13b8748e7c17fbe8cdc8430a0f181.2592000.1748155830.282335-118650883', 'scope': 'public audio_voice_assistant_get audio_tts_post brain_all_scope brain_enhanced_asr brain_speech_realtime brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '71839250416a64b057fdfb5a71269bbf'}\n",
      "SUCCESS WITH TOKEN: 24.9ab13b8748e7c17fbe8cdc8430a0f181.2592000.1748155830.282335-118650883 ; EXPIRES IN SECONDS: 2592000\n",
      "url is http://vop.baidu.com/server_api?cuid=123456PYTHON&token=24.9ab13b8748e7c17fbe8cdc8430a0f181.2592000.1748155830.282335-118650883&dev_pid=1537\n",
      "header is {'Content-Type': 'audio/wav; rate=16000', 'Content-Length': 200044}\n",
      "Request time cost 0.896316\n",
      "打开通讯工程。\n",
      "{\"corpus_no\":\"7497139564326100083\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"打开通讯工程。\"],\"sn\":\"904767357931745563830\"}\n",
      "\n",
      "语音识别结果 = 打开通讯工程。\n",
      "fetch token begin\n",
      "{\"refresh_token\":\"25.ac096a27bcf36b6836abce0c435df5f2.315360000.2060923832.282335-118650883\",\"expires_in\":2592000,\"session_key\":\"9mzdCSXggXr3Jeghn\\/gnSp5rdlykuKHWFBlCN14LNJ6WbJvJjeuAWig14zvOSlJB25zJ5YZDUnDs8gIkeL3zoJVWAzYC3KA=\",\"access_token\":\"24.23fd2167e4ee0f14c3d5f44ceef2b9da.2592000.1748155832.282335-118650883\",\"scope\":\"public audio_voice_assistant_get audio_tts_post brain_all_scope brain_enhanced_asr brain_speech_realtime brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"bccd676c9f31488bec5153b567ef3913\"}\n",
      "\n",
      "{'refresh_token': '25.ac096a27bcf36b6836abce0c435df5f2.315360000.2060923832.282335-118650883', 'expires_in': 2592000, 'session_key': '9mzdCSXggXr3Jeghn/gnSp5rdlykuKHWFBlCN14LNJ6WbJvJjeuAWig14zvOSlJB25zJ5YZDUnDs8gIkeL3zoJVWAzYC3KA=', 'access_token': '24.23fd2167e4ee0f14c3d5f44ceef2b9da.2592000.1748155832.282335-118650883', 'scope': 'public audio_voice_assistant_get audio_tts_post brain_all_scope brain_enhanced_asr brain_speech_realtime brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': 'bccd676c9f31488bec5153b567ef3913'}\n",
      "SUCCESS WITH TOKEN: 24.23fd2167e4ee0f14c3d5f44ceef2b9da.2592000.1748155832.282335-118650883 ; EXPIRES IN SECONDS: 2592000\n",
      "result saved as :result.wav\n",
      "********** Start playing! **********\n",
      "********** Stop playing! **********\n"
     ]
    }
   ],
   "source": [
    "#导入依赖包\n",
    "import asr_raw as ar  #百度语音识别包\n",
    "import webbrowser\n",
    "import tts as tts     #百度语音合成包\n",
    "from RecordPlay import recorder#录音和播放函数\n",
    "import os             #标准os库\n",
    "#录音loop,保存音频到test.wav文件\n",
    "r = recorder()\n",
    "r.recorder(\"test.wav\")\n",
    "#识别语音内容，并打印\n",
    "result_str = ar.Voice2Text(\"openwechat.wav\")\n",
    "print(\"语音识别结果 = \" + result_str)#打印识别结果\n",
    "#判断识别文本内容，并执行相应的操作\n",
    "if \"打开通\" in result_str:\n",
    "    os.startfile(\"calc.exe\")\n",
    "    text = \"主人，已经打开计算器。\"\n",
    "if \"讯\" in result_str:\n",
    "    os.startfile(\"notepad.exe\") \n",
    "    text = \"主人，已经打开记事本。\"\n",
    "if \"工\" in result_str:\n",
    "    os.startfile(\"mspaint.exe\")\n",
    "    text = \"主人，已经打开画图板。\"\n",
    "if \"打开通讯工程\" in result_str:\n",
    "    webbrowser.open(\"https://gitee.com/zhensi-rong\")\n",
    "    text = \"主人，已经打开gitee。\"\n",
    "elif \"你的名字\" in result_str:\n",
    "    text = \"我的名字是龙炜轩的助手。\"    \n",
    "else:\n",
    "    text = \"对不起，我还不能理解这个任务！\"\n",
    "#语音合成     \n",
    "file = tts.Text2Voice(text)\n",
    "#播放语音文件\n",
    "r.playwav(file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fordetect",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
